<?php

if (isset($_POST['action']) && !empty($_POST['action'])) {
    $action = $_POST['action'];

    switch ($action) {
        case 'checkUsername':
            if (isset($_POST['username']) && !empty($_POST['username'])) {
                $username = mres($_POST['username']);
                if (!isset($_SESSION['old_url'])) {
                    //popravka za smf proveru
                    $_SESSION['old_url'] = "1";
                }
                echo checkIfUsernameExist($username)?'true':'false';
            }
            else
                echo 'Username not sent';
            break;
        case 'checkEmail':
            if (isset($_POST['email']) && !empty($_POST['email'])) {
                $email = mres($_POST['email']);
                echo checkIfEmailExist($email)?'true':'false';
            }
            else
                echo 'Email not sent';
            break;
        case 'checkCaptcha':
            checkCaptcha();
            break;
        case 'register':
            echo portalRegister()?'true':'false';
            break;
        case 'checkIfJustRegisterWithSession':
            if(!empty($_SESSION['just_registered'])||empty($user_info['is_guest']))
            echo 'alreadyRegistered';
            break;
        default:
            break;
    }
}

function checkCaptcha() {
    require_once('class/recaptchalib.php');
    $privatekey = "6Le2w8cSAAAAADezyNhaMzyUQPGgIxmxSlM7m3Rv";
    $resp = recaptcha_check_answer($privatekey,
                    $_SERVER["REMOTE_ADDR"],
                    $_POST["recaptcha_challenge_field"],
                    $_POST["recaptcha_response_field"]);

    if (!$resp->is_valid) {
        // What happens when the CAPTCHA was entered incorrectly
        echo 'false';
    } else {
        // Your code here to handle a successful verification
        echo 'true';
    }
}

function portalRegister() {
    $user['datum_reg']= date("d m Y");
    $user['ime'] = mres($_POST['ime']);
    $user['username'] = mres($_POST['username']);
    $user['password'] = md5($_POST['password']);
    $user['email'] = mres($_POST['email']);
    $user['datum_rodj'] = mres($_POST['datum_rodj']);
    $user['grad'] = mres($_POST['grad']);
    $user['drzava'] = mres($_POST['drzava']);
    $user['telefon'] = mres($_POST['telefon']);
    $user['www'] = mres($_POST['www']);
    $user['studio'] = mres($_POST['studio']);
    $user['adresa_studija'] = mres($_POST['adresa_studija']);
    $user['telefon_studio'] = mres($_POST['telefon_studio']);
    $user['email_studio'] = mres($_POST['email_studio']);
    $user['www_studio'] = mres($_POST['www_studio']);
    $user['osebi'] = mres($_POST['osebi']);
    $user['prikaz_email'] = (int) $_POST['prikaz_email'];
    $user['prikaz_telefon'] = (int) $_POST['prikaz_telefon'];

    return portalDbRegister($user);

}



?>